<?php

require_once(WSB_MODULES_PATH . 'cms/models/Languages.php');
require_once(WSB_MODULES_PATH . 'cms/models/Language.php');

class Cms_Model_DbTable_Language extends WSB_Db_Table
{

    protected $_name = 'language';
    protected $_primary = 'id';
    protected $_rowsetClass = 'Cms_Model_Languages';
    protected $_rowClass = 'Cms_Model_Language';
    protected $_dependentTables = array();
	protected $_order = 'iso ASC';
	protected $_listcols = array('id', 'iso', 'language');

	/**
	 * @return SimpleXMLElement
	 */
	public function getSystemLanguages()
	{
        $path = WSB_APP_PATH . 'i18n/languages.xml';
        if(is_readable($path)) {
            $xml = simplexml_load_file($path);
            if($xml instanceof SimpleXMLElement) {
                return $xml;
            }
        }
	}

	/**
	 * Gib Administrations-Sprache zurueck.
	 *
	 * @return string
	 */
	public static function getAdminLanguage()
	{

		// Default Administrations-Sprache
		$language = Zend_Registry::get('Zend_Config')->application->defaultAdminLanguage;

		$auth = WSB_Auth_Backend::getInstance();
		if($auth->hasIdentity()) {
			$beuser = $auth->getIdentity();
			// Setze Benutzerspezifische Sprache
			if(!empty($beuser->admin_language)) {
				$table = new Cms_Model_DbTable_Language();
				foreach($table->getSystemLanguages() AS $item) {
					if(strtolower(strval($item->iso)) == $beuser->admin_language) {
						$language = $beuser->admin_language;
						break;
					}
				}
			}
		}
		return $language;

	}

}